#-*- coding: UTF-8 -*- 

from xml.dom import minidom
import time

def parse_timeline(xml_string):
    xmldoc = minidom.parseString(xml_string)
    
    sNode = xmldoc.firstChild
    
    # it's not graceful.待修改，会出现大问题。
    digu_list = [parse_status(status) for status in sNode.childNodes]
    return digu_list

def parse_status(status):

    digu_status = {
                    'created_at': status.getElementsByTagName('created_at')[0].firstChild and status.getElementsByTagName('created_at')[0].firstChild.data,
                    'id': status.getElementsByTagName('id')[0].firstChild and status.getElementsByTagName('id')[0].firstChild.data,
                    'text': status.getElementsByTagName('text')[0].firstChild and status.getElementsByTagName('text')[0].firstChild.data,
                    'picPath': status.getElementsByTagName('picPath') and status.getElementsByTagName('picPath')[0].firstChild.data,
                    'source': status.getElementsByTagName('source')[0].firstChild and status.getElementsByTagName('source')[0].firstChild.data,
                    'in_reply_to_status_id': status.getElementsByTagName('in_reply_to_status_id')[0].firstChild and status.getElementsByTagName('in_reply_to_status_id')[0].firstChild.data,
                    'in_reply_to_user_id': status.getElementsByTagName('in_reply_to_user_id')[0].firstChild and status.getElementsByTagName('in_reply_to_user_id')[0].firstChild.data,
                    'favorited': status.getElementsByTagName('favorited')[0].firstChild and status.getElementsByTagName('favorited')[0].firstChild.data,
                    'in_reply_to_user_name': status.getElementsByTagName('in_reply_to_user_name')[0].firstChild and status.getElementsByTagName('in_reply_to_user_name')[0].firstChild.data,
                    'user': {
                            'id': status.getElementsByTagName('user')[0].getElementsByTagName('id')[0].firstChild and status.getElementsByTagName('user')[0].getElementsByTagName('id')[0].firstChild.data,
                            'name': status.getElementsByTagName('user')[0].getElementsByTagName('name')[0].firstChild and status.getElementsByTagName('user')[0].getElementsByTagName('name')[0].firstChild.data,
                            'screen_name': status.getElementsByTagName('user')[0].getElementsByTagName('screen_name')[0].firstChild and status.getElementsByTagName('user')[0].getElementsByTagName('screen_name')[0].firstChild.data,
                            'location': status.getElementsByTagName('user')[0].getElementsByTagName('location')[0].firstChild and status.getElementsByTagName('user')[0].getElementsByTagName('location')[0].firstChild.data,
                            'description': status.getElementsByTagName('user')[0].getElementsByTagName('description')[0].firstChild and status.getElementsByTagName('user')[0].getElementsByTagName('description')[0].firstChild.data,
                            'profile_image_url': status.getElementsByTagName('user')[0].getElementsByTagName('profile_image_url')[0].firstChild and status.getElementsByTagName('user')[0].getElementsByTagName('profile_image_url')[0].firstChild.data,
                            'url': status.getElementsByTagName('user')[0].getElementsByTagName('url')[0].firstChild and status.getElementsByTagName('user')[0].getElementsByTagName('url')[0].firstChild.data,
                            'protected': status.getElementsByTagName('user')[0].getElementsByTagName('protected')[0].firstChild and status.getElementsByTagName('user')[0].getElementsByTagName('protected')[0].firstChild.data,
                            'gender': status.getElementsByTagName('user')[0].getElementsByTagName('gender')[0].firstChild and status.getElementsByTagName('user')[0].getElementsByTagName('gender')[0].firstChild.data,
                            'followers_count': status.getElementsByTagName('user')[0].getElementsByTagName('followers_count')[0].firstChild and status.getElementsByTagName('user')[0].getElementsByTagName('followers_count')[0].firstChild.data,
                            'friends_count': status.getElementsByTagName('user')[0].getElementsByTagName('friends_count')[0].firstChild and status.getElementsByTagName('user')[0].getElementsByTagName('friends_count')[0].firstChild.data,
                            'favourites_count': status.getElementsByTagName('user')[0].getElementsByTagName('favourites_count')[0].firstChild and status.getElementsByTagName('user')[0].getElementsByTagName('favourites_count')[0].firstChild.data,
                            'statuses_count': status.getElementsByTagName('user')[0].getElementsByTagName('statuses_count')[0].firstChild and status.getElementsByTagName('user')[0].getElementsByTagName('statuses_count')[0].firstChild.data,
                            'privacy': status.getElementsByTagName('user')[0].getElementsByTagName('privacy')[0].firstChild and status.getElementsByTagName('user')[0].getElementsByTagName('privacy')[0].firstChild.data,
                            } 
                   } 
    return digu_status

def parse_errorcode(xml_string):
    xmldoc = minidom.parseString(xml_string)
    return xmldoc.firstChild.getElementsByTagName('error')[0].firstChild.data

def parse_time(time_string):

    time_list = time_string.split()
    time_list.pop(4)
    time_tuple = time.strptime(' '.join(time_list), '%a %b %d %H:%M:%S %Y')
    #本来应该是：time_tuple = time.strptime(time_string, '%a %b %d %H:%M:%S %z %Y')，但这会得到ValueError: 'z' is a bad directive in format '%a %b %d %H:%M:%S %z %Y'


    second = time.mktime(time.localtime()) - time.mktime(time_tuple)
    if second < 60:
        result = str(int(second)) + 'sec.'
    else:
        minute = second/60
        if minute < 60:
            result = str(int(minute)) + 'min.'
        else:
            hour = minute/60
            if hour < 24:
                result = str(int(hour)) + 'hour'
            else:
                result = time_list[1] + ' ' + time_list[2]
    return result

